package dados;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import negocio.cliente.Cliente;
import negocio.cliente.ClienteExistenteException;
import negocio.cliente.IRepositorioCliente;





import connection.ConnectionUtil;

public class RepositorioClientesBDR implements IRepositorioCliente {

	private static final String INSERT_CLI =
		"INSERT INTO cliente (Cpf_cli, Nome_cli,Sexo_cli,Nasci_cli) VALUES (?,?,?,?)";

	private static final String UPDATE_CLI =
		"UPDATE cliente SET Nome_cli = ?, Cpf_cli, Sexo_cli, Nasci_cli WHERE cod_cli = ?";
	
	private static final String DELETE_CLI =
		"DELETE FROM cliente WHERE cod_cli = ?";

	private static final String CONSULTA_CLI =
		"SELECT COUNT(*) FROM cliente WHERE cod_cli = ?";

	private static final String LISTA_CLI =
		"SELECT * FROM cliente order by Nome_cli";

	private static final String PROCURA_CLI =
		"SELECT cod_cli,Nome_cli FROM cliente WHERE cod_cli = ?";
/**-------------------------------------------------------------------------------*/
	
	private static final String INSERT_END =
		"INSERT INTO endereco (Cod_end, CEP_end, Num_end, Compre_end) VALUES (?,?,?,?)";
	
	private static final String UPDATE_END =
		"UPDATE endereco SET cod_end = ?, CEP_end, Compre_end WHERE cod_end = ?";
/**-------------------------------------------------------------------------------*/	
	private static final String MSG_ERRO_ACESSO_BD =
		"Erro de acesso ao banco de dados";


	private Connection getConexao() throws SQLException, ClassNotFoundException {

		Connection con = null; 
		
		con = ConnectionUtil.getConnection();
		
		return con;
	}


	@Override
	public void atualizar(Cliente c) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean existe(String cpf) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public void inserir(Cliente c) throws ClienteExistenteException{

				if (c != null) {
					Connection con = null;
					PreparedStatement ps = null;
					try {
						con = getConexao();
						ps = con.prepareStatement(INSERT_CLI);
						ps.setString(1, c.getCpf());
						ps.setString(2, c.getNome());
						ps.setString(3, c.getSexo());
						ps.setString(4, c.getNascimento());
						ps.executeUpdate();
						} catch (SQLException e) {
							throw new ClienteExistenteException(INSERT_CLI);
							
						} catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			}
		
	}


	@Override
	public Cliente procurar(String cpf) {

		return null;
	}


	@Override
	public void remover(Cliente c) {
		if (c != null) {
			Connection con = null;
			PreparedStatement ps = null;
			try {
				con = getConexao();
				ps = con.prepareStatement(DELETE_CLI);
				ps.setInt(1, c.getCodCliente());
				ps.executeUpdate();
				} catch (SQLException e) {			
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
	}
}
}